import Vue from 'vue'
import Router from 'vue-router'
import store from '@/vuex'

import { sync } from 'vuex-router-sync'
const _import = require('./_import_' + process.env.NODE_ENV)
// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading
// 
Vue.use(Router)

/* Layout */

/**
* hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
* redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
* name:'router-name'             the name is used by <keep-alive> (must set!!!)
* meta : {
    role: ['admin','editor']     will control the page role (you can set multiple roles)
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
    noCache: true                if fasle ,the page will no be cached(default is false)
  }
**/
export const constantRouterMap = [
  { path: '/login',
    component: _import('login/index'), 
    hidden: true,
    meta:{ title: '登录', showTitle:true,showTabbar:false,noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/register',
    component: _import('login/register'),
    hidden: true,
    meta: { title: '手机快速注册', showTitle: true, showTabbar: false, noCache: false,
      rightConfig: {},
    }
  },
  // { path: '/404', component: _import('errorPage/404'), hidden: true },
  // { path: '/401', component: _import('errorPage/401'), hidden: true },
  {
    path: '/',
    name:'main',
    component:  _import('main'),
    meta:{ title: '蒸汽超市', showTitle:true,showSearch: true,showTabbar:true,showBack:false,noCache: false,
        leftConfig:{'icon':'location'},
    }
  },
  {
    path: '/culture',
    name: 'culture',
    component: _import('culture/index'),
    meta: {
      title: '蒸汽超市', showTitle: true, showSearch: true, showTabbar: true, showBack: false, noCache: false,
      leftConfig: { 'icon': 'location' },
    }
  },
  {
    path: '/shoplist',
    name:'shoplist',
    component:  _import('shop/index'),
    meta:{ title: '店铺列表', showTitle:false,showTabbar:false,showBack:true,noCache: false}
  },
  {
    path: '/shopDetail',
    name:'shopDetail',
    component:  _import('shop/shopDetail'),
    meta:{ title: '店铺详情', showTitle:true,showTabbar:false,showBack:true,noCache: false}
  },
  {
    path: '/goodslist',
    name:'goodslist',
    component:  _import('goods/index'),
    meta:{ title: '商品列表', showTitle:true,showTabbar:false,showBack:true,noCache: false,
      rightConfig:{'icon':'cart'},
    }
  },
  {
    path: '/goodsDetail',
    name:'goodsDetail',
    component:  _import('goods/goodsDetail'),
    meta:{ title: '商品列表', showTitle:false,showTabbar:false,showBack:true,noCache: false }
  },
  {
    path: '/goodsType',
    name: 'goodsType',
    component: _import('goods/type'),
    meta: { title: ' 产品分类', showTitle: true, showTabbar: false, showBack: true, noCache: false,
      rightConfig: { 'icon': 'cart' },
    }
  },
  {
    path: '/user',
    name:'user',
    component:  _import('user/user'),
    meta:{ title: '个人中心', showTitle:false,showTabbar:true,showBack:false,noCache: false}
  },
  {
    path: '/car',
    name: 'car',
    component: _import('car/index'),
    meta: { title: '购物车', showTitle: true, showSearch: false, showTabbar: true, showBack: false, noCache: false,
      rightConfig: { 'iconLable': '编辑', click: 'edit'},
    }
  },
  {
    path: '/address',
    name: 'address',
    component: _import('address/list'),
    meta: {
      title: '收货地址', showTitle: true, showSearch: false,showTabbar: false, showBack: false, noCache: false,
      rightConfig: { 'iconLable': '管理', click: 'edit'},
    }
  },
  {
    path: '/addressEdit',
    name: 'addressEdit',
    component: _import('address/edit'),
    meta: { title: '编辑收货地址', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig:{},
    }
  },
  {
    path: '/orderList',
    name: 'orderList',
    component: _import('order/list'),
    meta: {
      title: '订单管理', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/collect',
    name: 'collect',
    component: _import('order/collect'),
    meta: {
      title: '我的收藏', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/orderDetails',
    name: 'orderDetails',
    component: _import('order/details'),
    meta: {
      title: '订单详情', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  }, 
  {
    path: '/evaluate',
    name: 'evaluate',
    component: _import('order/evaluate'),
    meta: {
      title: '订单评价', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/orderConfirm',
    name: 'orderConfirm',
    component: _import('order/confirm'),
    meta: {
      title: '确认订单', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/orderPay',
    name: 'orderPay',
    component: _import('order/payMenu'),
    meta: {
      title: '支付方式', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/payOk',
    name: 'payOk',
    component: _import('order/payOk'),
    meta: {
      title: '支付成功', showTitle: true, showSearch: false, showTabbar: false, showBack: false, noCache: false,
      rightConfig: {},
    }
  },
  {
    path: '/404',
    name:'404',
    component:  _import('404'),
    meta:{ title: '404', showTitle:false,showTabbar:false,showBack:false,noCache: false,backUrl:'/'}
  },
  { path: '*', redirect: '/404', hidden: true }
]
const router = new Router({
  // mode: 'history', //后端支持可开
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})

// simple history management
const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)
router.beforeEach(function (to, from, next) {
  // store.commit('updateLoadingStatus', {isLoading: true})
  Vue.$vux.loading.show()
  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)
  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.dispatch('updateDirection', {direction: 'forward'})
    } else {
      store.dispatch('updateDirection', {direction: 'reverse'})
    }
  } else {
    ++historyCount
    history.setItem('count', historyCount)
    to.path !== '/' && history.setItem(to.path, historyCount)
    store.dispatch('updateDirection', {direction: 'forward'})
  }

  if (/\/http/.test(to.path)) {
    let url = to.path.split('http')[1]
    window.location.href = `http${url}`
  } else {
    next()
  }
})

router.afterEach(function (to) {
  Vue.$vux.loading.hide();
  // store.commit('updateLoadingStatus', {isLoading: false})
})
export default router

sync(store, router)
export const asyncRouterMap = [
  
  { path: '*', redirect: '/404', hidden: true }
]
